import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class J101202 {

    public static int[] smallestK(int[] arr, int k) {
        if(k == 0){
            return new int[]{};
        }

        PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        for(int i = 0; i < k; i++){
            pq.offer(arr[i]);
        }
        for(int j = k; j < arr.length; j++){
            if(arr[j] < pq.peek()){
                pq.poll();
                pq.offer(arr[j]);
            }
        }
        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = pq.poll();
        }
        return ret;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,3,5,7,2,4,6,8};
        System.out.println(Arrays.toString(smallestK(nums, 4)));
    }
}
